package com.xn.app.muying.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.xn.app.muying.R;
import com.xn.app.muying.adpter.DoctorRighttAdapter;
import com.xn.app.muying.bean.DoctorGsonBean;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/1.
 */
public class DoctorFragment extends Fragment implements HttpUtil.OnGetDataListener {

    private ListView lv;
    private Handler handler =  new Handler();
    private DoctorRighttAdapter doctorRighttAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.doctorright_fragment,container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv = (ListView) view.findViewById(R.id.doctor_fragment_right_listview);
        HttpUtil.getData(APIMethod.Home_Doctor_Left,handler,this);
        doctorRighttAdapter = new DoctorRighttAdapter(getActivity());
        lv.setAdapter(doctorRighttAdapter);
    }

    @Override
    public void onJsonData(String data) {
        ArrayList<DoctorGsonBean> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(data);
            JSONArray items = object.getJSONArray("items");
            for (int i = 0; i < items.length(); i++) {
                list.add(new Gson().fromJson(items.get(i).toString(),DoctorGsonBean.class));
            }
            doctorRighttAdapter.getData(list);
            Log.e("data22",list.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
